package com.thenie.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.thenie.pojo.User;
import com.thenie.service.UserService;
import com.thenie.utils.PasswordUtil;
import com.thenie.utils.SystemConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static com.thenie.utils.SystemConstant.LOGINUSER;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/register")
    public String register(User user,HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        if (userService.RegisterUser(user)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"恭喜你,注册成功!");

            User loginUser = userService.findUserByLoginName(user.getLoginName());
            session.setAttribute(LOGINUSER,loginUser);
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"很遗憾,注册失败,请重新尝试!");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 验证用户名是否存在
     * @param loginName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkLoginName")
    public String checkLoginName(String loginName){
        //创建Map集合，保存结果信息
        Map<String,Object> map = new HashMap<String,Object>();
        //调用根据用户名查询用户信息的方法
        if(userService.findUserByLoginName(loginName)!=null){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"用户名已存在！");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"用户名可用！");
        }
        return JSON.toJSONString(map);
    }

    @ResponseBody
    @RequestMapping("/login")
    public String login(String loginName, String password, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用登录的方法
        User loginUser = userService.login(loginName, password);
        //判断对象是否为空
        if(loginUser!=null){
            map.put(SystemConstant.SUCCESS,true);
            loginUser.setPassword(null);//清空
        //保存用户信息到会话中
            session.setAttribute(LOGINUSER,loginUser);
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"用户名或密码错误，请重新输入！");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/logout")
    public String login(HttpSession session){
        session.removeAttribute(LOGINUSER);

        return "redirect:/index.jsp";
    }

    @RequestMapping("/update")
    public String updateInfo(User user,HttpSession session){
        if (Objects.isNull(session.getAttribute(LOGINUSER))){
            return "redirect:/login.html";
        }

        if (StringUtil.isNotEmpty(user.getPassword())){
            String password = user.getPassword();

            String salt = UUID.randomUUID().toString().replace(",", "");
            String newPassword = PasswordUtil.md5(password, salt, SystemConstant.PASSWORD_COUNT);

            user.setPassword(newPassword);
            //设置加密后的密码
            user.setSalt(salt);
        }
        int i = userService.update(user);


        return "redirect:/userinfo.html";
    }

}
